#include "chatinterface.h"

chatinterface::chatinterface(QString myName, QString peerName,QWidget *parent)
    : QMainWindow{parent}
{
    this->myName = myName;
    this->peerNameStr = peerName;

    this->centralWidget = new QWidget(this);

    this->peerName = new QLabel(peerName);
    this->logChat = new QTextEdit(this);
    this->logChat->setReadOnly(true);
    this->msgSend = new QLineEdit(this);
    this->sendButton = new QPushButton("发送消息", this);
    this->recvButton = new QPushButton("接收消息", this);
    this->inviteButton = new QPushButton("邀请成员(管理员)");
    this->groupMemberList = new QListWidget(this);
    this->groupMemLabel = new QLabel("聊天成员", this);

    initData();
    initLayout();

    connect(sendButton, SIGNAL(clicked(bool)), this, SLOT(onSendButtonClicked()));
    connect(recvButton, SIGNAL(clicked(bool)), this, SLOT(onRecvButtonClicked()));
    connect(inviteButton, SIGNAL(clicked(bool)), this, SLOT(onInviteButtonClicked()));
}

void chatinterface::initData() {
    QFile logFile("logChat-user1.txt");
    if (logFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream in(&logFile);
        in.setCodec("UTF-8");
        // 读取文件内容并显示在TextEdit中
        this->logChat->setPlainText(in.readAll());

        // 关闭文件
        logFile.close();
    } else {
        // 处理文件无法打开的情况
        QMessageBox::warning(this, "警告", "聊天记录打开失败!");
    }

    QStringList itemList = {myName};
    if (this->peerNameStr[0] == 'u') {
        itemList.append(this->peerName->text());
        // itemList = {this->myName, this->peerName->text()};
    } else {
        itemList.append("user2");
        itemList.append("user3");
        itemList.append("user8");
        itemList.append("user12");
        itemList.append("user13");
    }

    this->groupMemberList->addItems(itemList);

}

void chatinterface::initLayout() {
    this->mainLayout = new QVBoxLayout;
    this->subLayout = new QHBoxLayout;
    this->subLayout2 = new QVBoxLayout;
    this->buttonLayout = new QHBoxLayout;

    this->subLayout->addWidget(logChat);
    this->subLayout->addLayout(subLayout2);

    this->subLayout2->addWidget(groupMemLabel);
    this->subLayout2->addWidget(groupMemberList);

    this->mainLayout->addWidget(peerName);
    this->mainLayout->addLayout(subLayout);
    this->mainLayout->addWidget(msgSend);

    //
    this->buttonLayout->addStretch();
    this->buttonLayout->addWidget(recvButton);
    this->buttonLayout->addWidget(sendButton);
    if (this->myName == "user1" && this->peerNameStr[0] == 'g') {
        this->buttonLayout->addWidget(inviteButton);
    }

    this->mainLayout->addLayout(buttonLayout);

    // this->mainLayout->addWidget(sendButton);

    this->setCentralWidget(centralWidget);
    centralWidget->setLayout(mainLayout);
}

void chatinterface::onSendButtonClicked() {
    qDebug() << "消息发送:" << getSendMsgString();
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString dateTime = currentDateTime.toString("yyyy-MM-dd HH:mm:ss ");
    QString finnalMsg = dateTime + myName + ":\n" + getSendMsgString();

    QFile file("logChat-user1.txt");
    if (file.open(QIODevice::Append | QIODevice::Text)) {
        QTextStream out(&file);
        out.setCodec("UTF-8");
        out << finnalMsg << Qt::endl;
        file.close();
    } else {
        QMessageBox::warning(this, "警告", "消息发送失败: 消息记录文件打开异常");
    }
    this->onRecvButtonClicked();
    this->msgSend->setText("");
}

void chatinterface::onRecvButtonClicked() {
    QFile file("logChat-user1.txt");
    if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream in(&file);
        in.setCodec("UTF-8");
        this->logChat->setPlainText(in.readAll());
        // 关闭文件
        file.close();
    } else {
        // 处理文件无法打开的情况
        QMessageBox::warning(this, "警告", "读取聊天记录失败");
    }
}

#include "QInputDialog"
void chatinterface::onInviteButtonClicked() {
    bool ok;
    QString inviteUser = QInputDialog::getText(this, "输入字符串", "请输入字符串:", QLineEdit::Normal, "", &ok);
    if (ok && !inviteUser.isEmpty()) {
        // 处理用户输入的字符串
        if (inviteUser.startsWith("user")) {
            this->groupMemberList->addItem(inviteUser);
        } else {
            QMessageBox::warning(this, "警告", "未找到用户!");
        }
    } else {
        QMessageBox::warning(this, "警告", "请输入内容！");
    }
}

QString chatinterface::getSendMsgString() {
    return this->msgSend->text();
}
